package com.hanj.myserver.utils;

import cn.hutool.setting.Setting;

import java.util.Objects;

/**
 * @author hanj.cn@outlook.com
 */
public class Initialization {

    private static Setting SETTING = new Setting(Constant.SETTING_FILEPATH);
    private static String PRIVATE_IP = "";
    private static String PUBLIC_IP = "";
    private static String USERNAME = "";
    private static String PASSWORD = "";
    private static String MSG_TO_NEW_USER = "";
    private static String IP_BARK = "";
    private static String DEVICE_CODE = "";
    private static String APP_TOKEN = "";
    private static String AUTHORIZATION = "";
    private static String TEST_REFRESH = "";
    private static String ANNOUNCEMENT = "";
    private static String ADMIN_PASSWORD = "";

    public static void reloadSetting() {

        SETTING.load();
        PRIVATE_IP = "";
        PUBLIC_IP = "";
        USERNAME = "";
        PASSWORD = "";
        MSG_TO_NEW_USER = "";
        IP_BARK = "";
        DEVICE_CODE = "";
        APP_TOKEN = "";
        AUTHORIZATION = "";
        TEST_REFRESH = "";
        ANNOUNCEMENT = "";
        ADMIN_PASSWORD = "";
    }

    public static String getAdminPassword() {

        if (Objects.equals(ADMIN_PASSWORD, "")) {
            ADMIN_PASSWORD = SETTING.getByGroup("ADMIN_PASSWORD", "杂项");
        }
        return ADMIN_PASSWORD;
    }

    public static String getAnnouncement() {

        if (Objects.equals(ANNOUNCEMENT, "")) {
            ANNOUNCEMENT = SETTING.getByGroup("ANNOUNCEMENT", "杂项");
        }
        return ANNOUNCEMENT;
    }

    public static String getTestRefresh() {

        if (Objects.equals(TEST_REFRESH, "")) {
            TEST_REFRESH = SETTING.getByGroup("TEST_REFRESH", "杂项");
        }
        return TEST_REFRESH;
    }

    public static String getAuthorization(boolean isNeedLogin) {

        boolean bool = Objects.equals(AUTHORIZATION, "") || isNeedLogin;
        if (bool) {
            AUTHORIZATION = MyUtil.getAuthorization(true);
        }
        return AUTHORIZATION;
    }

    public static String getIpBark() {

        if (Objects.equals(IP_BARK, "")) {
            IP_BARK = SETTING.getByGroup("IP_BARK", "bark");
        }
        return IP_BARK;
    }

    public static String getDeviceCode() {

        if (Objects.equals(DEVICE_CODE, "")) {
            DEVICE_CODE = SETTING.getByGroup("DEVICE_CODE", "bark");
        }
        return DEVICE_CODE;
    }

    public static String getAppToken() {

        if (Objects.equals(APP_TOKEN, "")) {
            APP_TOKEN = SETTING.getByGroup("APP_TOKEN", "WxPusher");
        }
        return APP_TOKEN;
    }

    public static String getIp() {

        boolean usePublicIp = Boolean.parseBoolean(SETTING.getByGroup("USE_PUBLIC_IP", "服务器IP"));
        if (usePublicIp) {
            return getPublicIp();
        }
        return getPrivateIp();
    }

    public static String getPrivateIp() {

        if (Objects.equals(PRIVATE_IP, "")) {
            PRIVATE_IP = SETTING.getByGroup("PRIVATE_IP", "服务器IP");
        }
        return PRIVATE_IP;
    }

    public static String getPublicIp() {

        if (Objects.equals(PUBLIC_IP, "")) {
            PUBLIC_IP = SETTING.getByGroup("PUBLIC_IP", "服务器IP");
        }
        return PUBLIC_IP;
    }

    public static String getUsername() {

        if (Objects.equals(USERNAME, "")) {
            USERNAME = SETTING.getByGroup("USERNAME", "青龙账密");
        }
        return USERNAME;
    }

    public static String getPassword() {

        if (Objects.equals(PASSWORD, "")) {
            PASSWORD = SETTING.getByGroup("PASSWORD", "青龙账密");
        }
        return PASSWORD;
    }

    public static String getMsgToNewUser() {

        if (Objects.equals(MSG_TO_NEW_USER, "")) {
            MSG_TO_NEW_USER = SETTING.getByGroup("MSG_TO_NEW_USER", "杂项");
        }
        return MSG_TO_NEW_USER;
    }

}
